package com.cencat.common.enums;

/**
 * 订单状态枚举
 * 
 * @author cencat
 * @since 2024-01-01
 */
public enum OrderStatus {

    /**
     * 待支付
     */
    PENDING_PAYMENT(1, "待支付"),

    /**
     * 已支付
     */
    PAID(2, "已支付"),

    /**
     * 待派单
     */
    PENDING_DISPATCH(3, "待派单"),

    /**
     * 已派单
     */
    DISPATCHED(4, "已派单"),

    /**
     * 司机已接单
     */
    ACCEPTED(5, "司机已接单"),

    /**
     * 配送中
     */
    IN_DELIVERY(6, "配送中"),

    /**
     * 已送达
     */
    DELIVERED(7, "已送达"),

    /**
     * 已完成
     */
    COMPLETED(8, "已完成"),

    /**
     * 已取消
     */
    CANCELLED(9, "已取消"),

    /**
     * 退款中
     */
    REFUNDING(10, "退款中"),

    /**
     * 已退款
     */
    REFUNDED(11, "已退款"),

    /**
     * 异常
     */
    EXCEPTION(12, "异常");

    /**
     * 状态码
     */
    private final Integer code;

    /**
     * 状态描述
     */
    private final String description;

    /**
     * 构造函数
     * 
     * @param code 状态码
     * @param description 状态描述
     */
    OrderStatus(Integer code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 获取状态码
     * 
     * @return 状态码
     */
    public Integer getCode() {
        return code;
    }

    /**
     * 获取状态描述
     * 
     * @return 状态描述
     */
    public String getDescription() {
        return description;
    }

    /**
     * 根据状态码获取枚举
     * 
     * @param code 状态码
     * @return 订单状态枚举
     */
    public static OrderStatus getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (OrderStatus status : OrderStatus.values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        return null;
    }

    /**
     * 判断是否可以取消
     * 
     * @return 是否可以取消
     */
    public boolean canCancel() {
        return this == PENDING_PAYMENT || this == PAID || this == PENDING_DISPATCH;
    }

    /**
     * 判断是否可以退款
     * 
     * @return 是否可以退款
     */
    public boolean canRefund() {
        return this == PAID || this == PENDING_DISPATCH || this == DISPATCHED || this == ACCEPTED;
    }

    /**
     * 判断是否为进行中状态
     * 
     * @return 是否进行中
     */
    public boolean isInProgress() {
        return this == PENDING_PAYMENT || this == PAID || this == PENDING_DISPATCH || 
               this == DISPATCHED || this == ACCEPTED || this == IN_DELIVERY;
    }

    /**
     * 判断是否为终态
     * 
     * @return 是否为终态
     */
    public boolean isFinal() {
        return this == COMPLETED || this == CANCELLED || this == REFUNDED;
    }

    /**
     * 判断是否需要司机处理
     * 
     * @return 是否需要司机处理
     */
    public boolean needDriverAction() {
        return this == DISPATCHED || this == ACCEPTED || this == IN_DELIVERY;
    }
}